package com.example.rbacmanage.vo;

import com.example.rbacmanage.entity.system.Role;
import com.example.rbacmanage.entity.system.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class MyUserDetails implements UserDetails {
    private String username;
    private String password;
    private Integer id;
    /**
     * 过期时间
     */
    private Long exipredTime;
    private User user;
    private List<Role> roles;

    public Long getExipredTime() {
        return exipredTime;
    }

    public void setExipredTime(Long exipredTime) {
        this.exipredTime = exipredTime;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }


    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public MyUserDetails(User user) {
        this.user = user;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (roles == null){
            return null;
        }
        List<String> permissions = new ArrayList<>();
        roles.forEach(role -> {
            permissions.addAll(role.getPermissions());
        });
        return permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }
}
